package com.qst;

import com.qst.pojo.User;
import com.qst.pojo.Userlist;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SysInterceptor implements HandlerInterceptor {
	 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			 Object handler) throws Exception {
		  // 在拦截点执行前拦截，如果返回true则不执行拦截点后的操作（拦截成功）
		  // 返回false则不执行拦截
		  HttpSession session = request.getSession();
		  //String uri = request.getRequestURI(); // 获取登录的uri，这个是不进行拦截的
		  //if(session.getAttribute("LOGIN_USER")!=null || uri.indexOf("system/login")!=-1) {// 说明登录成功 或者 执行登录功能
		  User user = (User) session.getAttribute("user");
		  Userlist list = (Userlist) session.getAttribute("ulist");
		  if (user != null) {
				// 没有完善信息，则进行拦截
				if (!"/userlist/get.action".equals(request.getServletPath())
						&& !"/userlist/update.action".equals(request.getServletPath())) {
					 if (list == null || list.getPhone() == null || list.getName() == null
							 || list.getIdcard() == null) {
						  response.sendRedirect(request.getContextPath() + "/userlist/get.action");
						  return false;
					 }
				}
				// 登录成功不拦截
				return true;
		  } else {
				// 拦截后进入登录页面
				System.out.println("进入登录页。。。");
				response.sendRedirect(request.getContextPath() + "/login.action");
				return false;
		  }

	 }

	 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response,
			 Object handler, ModelAndView modelAndView) throws Exception {

	 }

	 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
			 Object handler, Exception ex) throws Exception {

	 }
}
